from django.db import models
from core.models import CoreModel, table_prefix
import reversion

"""产品模块对象"""


@reversion.register()
class Module(CoreModel):
    product = models.ForeignKey(
        to="pms.Product",
        related_name="module_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="所属产品",
    )
    owner = models.ForeignKey(
        null=True,
        blank=True,
        to="sys.User",
        related_name="module_owner_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="负责人",
    )
    parent = models.ForeignKey(
        null=True,
        blank=True,
        to="Module",
        related_name="module_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="父模块",
    )
    name = models.CharField(max_length=128, verbose_name="模块名称")
    number = models.CharField(max_length=128, verbose_name="模块编号")
    script = models.TextField(null=True, blank=True, verbose_name="执行脚本")

    def __str__(self) -> str:
        return self.name

    class Meta:
        unique_together = (("product", "name"),)
        db_table = table_prefix + "pms_module"
        verbose_name = "模块管理"
        verbose_name_plural = verbose_name
        ordering = ("-update_time",)
